package com.example.forestmanagement.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.forestmanagement.model.LocationPoint;
import java.util.ArrayList;
import java.util.List;

public class LocationDBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "location.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_LOCATIONS = "locations";
    
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_USER_ID = "user_id";
    private static final String COLUMN_LATITUDE = "latitude";
    private static final String COLUMN_LONGITUDE = "longitude";
    private static final String COLUMN_CREATE_TIME = "create_time";
    private static final String COLUMN_IS_SYNCED = "is_synced";

    public LocationDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE " + TABLE_LOCATIONS + "("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_USER_ID + " INTEGER,"
                + COLUMN_LATITUDE + " REAL,"
                + COLUMN_LONGITUDE + " REAL,"
                + COLUMN_CREATE_TIME + " INTEGER,"
                + COLUMN_IS_SYNCED + " INTEGER DEFAULT 0"
                + ")";
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATIONS);
        onCreate(db);
    }

    public void addLocation(LocationPoint location) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_USER_ID, location.getUserId());
        values.put(COLUMN_LATITUDE, location.getLatitude());
        values.put(COLUMN_LONGITUDE, location.getLongitude());
        values.put(COLUMN_CREATE_TIME, location.getCreateTime());
        db.insert(TABLE_LOCATIONS, null, values);
        db.close();
    }

    public List<LocationPoint> getUnsyncedLocations() {
        List<LocationPoint> locations = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + TABLE_LOCATIONS + " WHERE " + COLUMN_IS_SYNCED + " = 0";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                LocationPoint location = new LocationPoint();
                location.setId(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
                location.setUserId(cursor.getInt(cursor.getColumnIndex(COLUMN_USER_ID)));
                location.setLatitude(cursor.getDouble(cursor.getColumnIndex(COLUMN_LATITUDE)));
                location.setLongitude(cursor.getDouble(cursor.getColumnIndex(COLUMN_LONGITUDE)));
                location.setCreateTime(cursor.getLong(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
                locations.add(location);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return locations;
    }

    public void markLocationAsSynced(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_IS_SYNCED, 1);
        db.update(TABLE_LOCATIONS, values, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
        db.close();
    }
} 